﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace MLBService.Resources
{
    [DataContract]
    public class TeamList
    {
        #region Properties

        [DataMember]
        public List<UriLink> Teams = new List<UriLink>();

        #endregion

        public TeamList Load(int year)
        {
            using (Model.fourseamEntities context = new Model.fourseamEntities())
            {
                var teams = (
                    from ft in context.FieldedTeams
                    from nn in context.TeamNickNames
                    where nn.TeamNickNameId == ft.TeamNickNameId
                    && ft.Yr == year
                    orderby nn.NickName
                    select new
                    {
                        teamId = ft.FieldedTeamId,
                        nickname = nn.NickName
                    }).ToList();
                
                foreach (var t in teams)
                {
                    Teams.Add(new UriLink(string.Format(Constants.baseUrl + "team/{0}", t.teamId), t.nickname));
                }
                return this;
            }            
        }
    }
}